﻿using System;
using System.Data;
using Pharmacy;
using Pharmacy.Data;
using Pharmacy.Entities;

namespace Main.Forms
{
    public partial class MiniFormUsers : DevExpress.XtraEditors.XtraForm
    {
        public MiniFormUsers()
        {
            InitializeComponent();
            gridControl1.DataSource = DataRepository.WebUserProvider.GetAll();
            DataTable listRoles = new DataTable();
            listRoles.Columns.Add("Roles");
            //Dictionary<string, string> =new Dictionary<string, string>();
            string[] Roles = { "Pharmacist", "Manager", "Admin" };
            foreach (string role in Roles)
            {
                DataRow r = listRoles.NewRow();
                r["Roles"] = role;
                listRoles.Rows.Add(r);
            }
            cbo_Roles.Properties.DisplayMember = "Roles";
            cbo_Roles.Properties.ValueMember = "Roles";
            cbo_Roles.Properties.DataSource = listRoles;
        }

        private void btn_Add_Click(object sender, EventArgs e)
        {
            if (btn_Add.Text == "Add")
            {
                tb_UserID.Text = "";
                tb_UserID.Enabled = true;
                tb_FullName.Text = "";
                tb_FullName.Enabled = true;
                cbo_Roles.EditValue = "";
                cbo_Roles.Enabled = true;
                btn_Add.Text = "Save";
                btn_Edit.Text = "Cancel";
            }
            else if (btn_Add.Text == "Save")
            {
                if (tb_UserID.Text != "")
                {
                    WebUser objUser = new WebUser();
                    objUser.UserId = tb_UserID.Text;
                    objUser.Name = tb_FullName.Text;
                    objUser.Roles = cbo_Roles.EditValue.ToString();
                    DataRepository.WebUserProvider.Save(objUser);
                }
                btn_Edit_Click(sender, e);
            }
            else
            {
                WebUser objUser = DataRepository.WebUserProvider.GetByUserId(tb_UserID.Text);
                objUser.Roles = cbo_Roles.EditValue.ToString();
                objUser.Name = tb_FullName.Text;
                DataRepository.WebUserProvider.Update(objUser);
                if (Program.GlobalUser.UserId == objUser.UserId)
                    Program.GlobalUser = objUser;
                btn_Edit_Click(sender, e);
            }
        }

        private void btn_Edit_Click(object sender, EventArgs e)
        {
            if (btn_Edit.Text == "Edit")
            {
                WebUser objUser = (WebUser)grvUsers.GetRow(grvUsers.GetSelectedRows()[0]);
                if (objUser != null)
                {
                    tb_UserID.Text = objUser.UserId;
                    tb_FullName.Enabled = true;
                    cbo_Roles.Enabled = true;
                    tb_FullName.Text = objUser.Name;
                    cbo_Roles.EditValue = objUser.Roles;
                    btn_Add.Text = "Update";
                    btn_Edit.Text = "Cancel";
                }
            }
            else
            {
                gridControl1.DataSource = DataRepository.WebUserProvider.GetAll();
                tb_UserID.Text = "";
                tb_FullName.Text = "";
                cbo_Roles.EditValue = "";
                tb_UserID.Enabled = false;
                tb_FullName.Enabled = false;
                cbo_Roles.Enabled = false;
                btn_Add.Text = "Add";
                btn_Edit.Text = "Edit";
            }
        }
    }
}